package com.meicloud.license.appl;

import com.meicloud.license.appl.utils.LicenseClientUtils;
import com.meicloud.license.appl.vo.LicenseDto;
import com.meicloud.license.appl.vo.LicenseRegisterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * license模拟测试类
 *
 * @author zhanghui145
 * @date 2022/1/10 17:16
 * @since 1.0.0
 */
@Component
public class LicenseMock {

    @Autowired
    private LicenseClientUtils licenseClientUtils;

    private static final String NAME = "meiCloud";

    private static final Map<String, LicenseDto> CACHE = new HashMap<>();

    public String generateRegisterCode() {
        LicenseRegisterDto registerDto = new LicenseRegisterDto();
        registerDto.setName(NAME);
        registerDto.setSysName("license-demo");
        registerDto.setSysIp("localhost");
        registerDto.setSysVersion("V1");
        registerDto.setDuration(365);
        return licenseClientUtils.generateRegisterCode(registerDto);
    }

    public String generateLicenseCode(String registerCode) {
        return licenseClientUtils.generateLicenseCode(registerCode);
    }

    public boolean customRegister(String registerCode, String licenseCode) {
        return licenseClientUtils.customRegister(registerCode, licenseCode);
    }

    public LicenseDto licenseStatus() {
        return CACHE.computeIfAbsent(NAME, k ->
                licenseClientUtils.licenseStatus(k, "localhost", "license-demo"));
    }
}
